package org.sociald.wave;

import org.sociald.facebook.client.FacebookClient;
import com.google.code.facebookapi.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

/**
 * Created by IntelliJ IDEA.
 * User: jackie
 * Date: Dec 12, 2009
 * Time: 9:05:32 PM
 * To change this template use File | Settings | File Templates.
 */
public class FacebookLoginRequest extends HttpServlet {
    private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";
    private static final String FACEBOOK_API_KEY = "3f0d311e56d6af31a74603e142997063";
    private static final String FACEBOOK_SECRET = "26552151c07e0552c6fca69818c10e13";

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String loginPage = "http://www.facebook.com/login.php?api_key=" + FACEBOOK_API_KEY + "&v=1.0&canvas=true";

        String sessionKey = null;
        sessionKey = req.getParameter(FacebookParam.SESSION_KEY.toString());

        // Session Key passed as request parameter
        if (sessionKey == null) {
            // If there is not session key, they user not logged in
            System.out.println("session key not found");
            //servletOutput.println("session expired");
            // Facebook Redirect to login page
            resp.sendRedirect(loginPage); //loginPage string defined earlier
        } else {
            Object user = req.getParameter("fb_sig_user");

            // get user as a string. User info passed as request parameter
            System.out.println("User is " + user);
            // displays numeric value
            FacebookClient facebook = new FacebookClient();
            facebook.setClient(sessionKey);
            // create Facebook Json Rest Client


            System.out.println("Facebook Client created");
            // with Facebook client created, now Facebook API calls can be made
            // In this case, a call to FQL to get username
            try {
                //This needs to be changed to reflect the date since the last check (just get new messages)
                DateTime test =new DateTime(2009,12,01,0,0,0,0,DateTimeZone.UTC);
                facebook.getMessagesByDate(user.toString(),test);
                
                //System.out.println("Friend Name is " + friendsListArray+ "<br>");
            }
            catch (FacebookException ex) {
                System.out.println(">Error: Couldn't talk to Facebook> " + ex);
                String message = ex.toString();
                if (ex.toString().equals("com.google.code.facebookapi.FacebookException: stream requires the read_stream extended permission.")) {
                    String givePermissions = "http://www.facebook.com/connect/prompt_permissions.php?api_key=" + FACEBOOK_API_KEY + "&v=1.0&next=http://192.168.123.102:8080/facebookloginrequest&display=popup&ext_perm=read_stream,publish_stream&enable_profile_selector=1&profile_selector_ids=" + user;
                    resp.sendRedirect(givePermissions);
                }
                String test = "other";
            }
        }
    }
}
